#!/usr/bin/python
# encoding=utf-8

import time
import requests
from wm.etc.config import ASCONFIG
from wm.lib.loglib import log_error


class AScanner(object):
    def __init__(self, username, password):        
        self.username = username
        self.password = password        

    
    def addTask(self, url, callback=ASCONFIG['CALLBACK'], maxconnect=6, types='deep', agent=None, proxy=None):        
        '''            
        @param url               url=http://www.host.com 要扫描的url            
        @param maxConnect                每秒钟HTTP并发请求数量，该值要大于等于3            
        @param type                扫描策略，可以为normal（正常扫描）或deep（深度扫描）           
        @param agent               【可选参数】发送HTTP请求时添加的User-Agent            
        @param proxy               【可选参数】扫描器的代理IP            
        @param callback                 【可选参数】扫描结束后，扫描器会把该url中的关键字"$taskid"替换为真正的任务id,然后访问这个url；                 
                    如果callback是邮箱，会发送扫描邮件到该邮箱。            
        @return 创建成功：返回一个int值，即该任务的ID。 创建失败：返回一个字符串，即失败原因。      
        '''        
        paramdic = {}       
        paramdic['act'] = 'add'        
        paramdic['url'] = url
        paramdic['callback'] = callback         
        paramdic['maxConnect'] = maxconnect        
        paramdic['type'] = types        
        
        if agent is not None:            
            paramdic['agent'] = agent        
        if proxy is not None:            
            paramdic['proxy'] = proxy                
                             
        try:           
            result = self.sendRequest(paramdic)    
            if result:
                return str(1),result
            else:
                return str(0),''  
        except Exception as e:
            log_error("addTask():"+str(e))           
            return str(0),''       
          
    def cancelTask(self, id):
        paramdic = {}
        paramdic['act'] = 'cancel'
        if id:
            paramdic['id'] = id
            
        try:       
            result = self.sendRequest(paramdic)
            if result:
                return str(1),result
            else:
                return str(0),''
        except Exception as e:
            log_error("getTaskList():"+str(e))           
            return str(0),'' 
        
    def getTaskList(self):
        paramdic = {}
        paramdic['act'] = 'list'
        
        try:       
            result = self.sendRequest(paramdic)
            if result:
                return str(1),result
            else:
                return str(0),''
        except Exception as e:
            log_error("getTaskList():"+str(e))           
            return str(0),'' 
        
    def getScanningResult(self, ids):
        paramdic = {}
        paramdic['act'] = 'get'
        paramdic['format'] = 'json'
        if ids is not None:
            paramdic['ids'] = ids
        
        try:       
            result = self.sendRequest(paramdic)
            if result:
                return str(1),result
            else:
                return str(0),''
        except Exception as e:
            log_error("getScanningResult():"+str(e))           
            return str(0),'' 
                         
    def sendRequest(self, paramdic):        
        '''
        向安塞漏洞扫描仪器发送扫描请求
        '''        
            
        try:
            paramdic['username'] = self.username
            paramdic['password'] = self.password
            paramdic['t'] = str(int(time.time()))
            r = requests.get(url=ASCONFIG['ASURL'], params=paramdic)
            return r.content  
        except Exception as e:
            log_error("sendRequest():"+str(e))


 
if __name__ == '__main__':
    scanner = AScanner(ASCONFIG['USERNAME'],ASCONFIG['PASSWORD'])
    #print scanner.addTask("http://www.baidu.com", "callback")
    
    ids = '952173'
    print scanner.getScanningResult(ids)[1]
    
     
        
        
        
        